const events = require("events");
const EventEmitter = events.EventEmitter;

//dog的构造函数
function Dog(dogName, energy) {
    //构造函数对象添加属性
    this.dogName = dogName;
    this.energy = energy;
    //执行EventEmitter函数，调用主体是this
    EventEmitter.call(this);
    this.on("bark", () => {
        if (this.energy >= 0) {
            console.log(this.dogName + " is barking,energy is " + this.energy);
        } else {
            process.exit();
        }
        this.energy--;
    })
}

Dog.prototype = EventEmitter.prototype;

let dog = new Dog("taidi", 5);
let dogy = new Dog("zangao", 8)
let intervalId = setInterval(() => {
    dog.emit("bark");
    dogy.emit("bark");
}, 1000)
// dog.emit("bark");